package artid.com.arttogo.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;

import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.ArtistFollow;
import artid.com.arttogo.bean.ArtistList;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.BitmapCache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.MyBitmapImageViewTarget;

public class AttentionFollowAdapter extends BaseAdapter {
	private Context mContext;
	LayoutInflater mInflater;
	ViewHolder holder;
	public List<ArtistList> mList;
	private ImageLoader mImageLoader;

	public AttentionFollowAdapter(Context mContext, List<ArtistList> bean) {
		
		this.mContext = mContext;
		mInflater = LayoutInflater.from(mContext);
		this.mList = bean;
		RequestQueue mQueue = Volley.newRequestQueue(mContext);
		mImageLoader = new ImageLoader(mQueue, new BitmapCache());
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	
	public void addData(List<ArtistList> list){
		mList.addAll(list);
		notifyDataSetChanged();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_personal_message,
					null);
			vh.name = (TextView) convertView
					.findViewById(R.id.item_attention_name);
			vh.gz = (TextView)convertView.findViewById(R.id.item_attention_gz);
			vh.zan = (TextView) convertView.findViewById(R.id.text_attention_z);
			vh.collect = (TextView) convertView
					.findViewById(R.id.item_attention_collect);
			vh.icon = (ImageView)convertView.findViewById(R.id.item_attention_photo);
			vh.btngz = (TextView)convertView.findViewById(R.id.text_attention_gz);
			vh.btnqxgz = (TextView)convertView.findViewById(R.id.text_attention_qxgz);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		
		ArtistList itemFollow = mList.get(position);
		vh.btngz.setOnClickListener(listenerGz(position,itemFollow.getId(), true));
		if (itemFollow.getIs_follow().equals("1")) {
			vh.btngz.setVisibility(View.GONE);
			vh.btnqxgz.setVisibility(View.VISIBLE);
			vh.btnqxgz.setOnClickListener(listenerGz(position,itemFollow.getId(), false));
		}else {
			vh.btngz.setVisibility(View.VISIBLE);
			vh.btnqxgz.setVisibility(View.GONE);
		}
		
		vh.name.setText(itemFollow.getRealname());
		vh.gz.setText("关注 " +itemFollow.getFollow_num());
		vh.zan.setText("点赞 " + itemFollow.getCommend_num());
		vh.collect.setText("藏品 " + itemFollow.getArt_num());
			Glide.with(mContext).load(itemFollow.getImgurl()+ "_" + "100" + "x10.png").asBitmap().centerCrop()
		.placeholder(R.drawable.ic_menu_rotate).error(R.drawable.ic_menu_rotate)
		.into(new MyBitmapImageViewTarget(vh.icon));
		
		
		return convertView;
	}
	View.OnClickListener listenerGz(final int indexs, final String id, final Boolean setFollow){
		return new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			
					reqData(indexs,id,setFollow);
				
			}
		};
	}

	void reqData(final int index, String id, final Boolean setFollow){
		String state = "";
		if (setFollow) {
			state = "1";
		}else {
			state = "2";
		}
		VolleyBasic.getInstanceNoDialog((Activity)mContext).post(Cache.artistFollow,
				VolleyMap.getInstance().artistFollow(AppUtil.getUser().getToken(), id, state), ArtistFollow.class,
				new VolleyCallback() {
					
					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						ArtistFollow itemArtistFollow  = (ArtistFollow)vd.getData();
						
						if (itemArtistFollow.getState().equals("1")) {
							ArtistList item = mList.get(index);
							if (setFollow) {
								item.setIs_follow("1");
							}else {
								item.setIs_follow("2");
							}
							mList.set(index, item);
							notifyDataSetChanged();
							
//							 reqlistData();
						}
					}
					
					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						
					}
				});
	}
	

	
	class ViewHolder {

		TextView name, gz, btngz, zan, collect, btnqxgz;
		ImageView icon, pic;

	}
}
